<?php

class Admin_PageController extends Zend_Controller_Action
{
    
    public function init()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->role = $auth->getIdentity()->role;
            $this->uid = $auth->getIdentity()->id;
        }
        else{
            $this->_redirect('/user/login');
        }
    }

    /**
     * 后台文章管理首页
     * 
     */
    public function homeAction()
    {
        // 取得分页适配器
        $modelPage = new Admin_Model_Page();
        if($this->role != 'admin'){
            $branch = $this->cid;
        }
        $adapter = $modelPage->getPages();
        $paginator = new Zend_Paginator($adapter);
        $perPage = 20;// 每页显示篇文章
        $paginator->setItemCountPerPage($perPage);
        // 默认显示的分页页码
        $page = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($page);
        // 渲染分页适配器 
        $this->view->paginator = $paginator;
        
        $this->view->perPage = $perPage;
    }

    /**
     * 创建文章
     * 
     * @throws Zend_Exception
     */
    public function createAction()
    {
        $type = $this->_request->getParam('type');// 获得页面类型
        switch ($type){
            case 'about':
                $fid = 1;
                break;
            case 'news':
                $fid = 3;
                break;
            case 'blog':
                $fid = 5;
                break;
            default:
                $fid = 0;
        }
        $formPage = new Admin_Form_Page(0,$fid); //实例化的同时传递页面类型参数给表单
        if($this->getRequest()->isPost()){
            if($formPage->isValid($_POST)){
                $pageModel = new Ms_Model_Page();
                $data = $formPage->getValues();
                $data['type'] = $type;
                $data['uid'] = $this->uid;
                $data['createtime'] = time();
                $data['updatetime'] = time();
                $newPage = $pageModel->createPage($data);

                    if($newPage){
                        $this->_redirect('/admin/page/list/type/'.$type);
                    }
                    else{
                        throw new Zend_Exception('添加页面出错！');
                    }
            }
        }
        $this->view->formPage = $formPage;
        $this->view->type = $type;
    }

    /**
     * 更新文章
     * 
     * @throws Zend_Exception
     */
    public function updateAction()
    {
        $id = $this->_request->getParam('id');
        $modelPage = new Ms_Model_Page();
        $currentPage = $modelPage->find($id)->current();
        $formPage = new Admin_Form_Page($currentPage->cid);
        if($this->getRequest()->isPost()){
            if($formPage->isValid($_POST)){
                $data = $formPage->getValues();
                $data['uid'] = $this->uid;
                $data['updatetime'] = time();
                $page = $modelPage->updatePage($id, $data);
                if($page){
                    $this->_redirect('/admin/page/list/type/'.$currentPage->type);
                }
                else{
                    throw new Zend_Exception('编加页面出错！');
                }
            }
        }
        $formPage->populate($currentPage->toArray());
        $this->view->formPage = $formPage;
        $this->view->type = $currentPage->type;
    }

    /**
     * 删除文章
     * 
     */
    public function deleteAction()
    {
        $id = $this->_request->getParam('id');
        $pageModel = new Ms_Model_Page();
        $pageDelete = $pageModel->deletePage($id);
        $this->_redirect('/admin/page/list/type/blog');
    }

    /**
     * 文章列表
     * 
     * 包含文章分类过滤器以筛选文章
     * 
     */
    public function listAction()
    {
        $type = $this->_request->getParam('type');// 获得页面类型
        switch ($type){
            case 'about':
                $fid = 1;
                break;
            case 'news':
                $fid = 3;
                break;
            case 'blog':
                $fid = 5;
                break;
            default:
                $fid = 0;
        }
        
        // 获取过滤表单工具条
        $pageListTool = new Admin_Form_PageListTool($fid);
        $pageListTool->setMethod('get');
        $this->view->pageListTool = $pageListTool;
        // 排序和过滤规则.
        $filter = $this->_request->getParam('filter', null);
        $sort = $this->_request->getParam('sort', null);
        // 设置表单值
        $pageListTool->getElement('filter')->setValue($filter);
        $pageListTool->getElement('sort')->setValue($sort);
        
        $modelPage = new Admin_Model_Page();
        
        if($filter){
            $where = array('type'=>$type, 'cid'=>$filter);
        }
        else{
            $where = array('type'=>$type);
        }
        if($sort){
            $pages = $modelPage->listPages($where, $sort);
        }
        else{
            $pages = $modelPage->listPages($where);
        }
  
        $pages = $modelPage->listPages($where, $sort);
        
        $paginator = new Zend_Paginator($pages);
        $perPage = 5;// 每页显示篇文章
        $paginator->setItemCountPerPage($perPage);
        // 默认显示的分页页码
        $page = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($page);
        // 渲染分页适配器
        $this->view->pages = $paginator;
        $this->view->type = $type;
    }
}
